<?php
/* ******************************
 *	letterA aJax actions
 * ******************************
 */
$include    = '';
$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];
switch ($action) {
    case 'get_edit_page':
        $l_id=explode('-',$_REQUEST[id]);
        $_REQUEST[id]=$l_id[1];
        if ($l_id[0]=='A') {
            $include = 'letterA.action.php';
        }elseif ($l_id[0]=='D'){
            $include = 'letterD.action.php';
        }
        break;
    case "get_end_date":
        $include = 'letterA.action.php';
        break;
        
    case "trans_type":
         require_once ('../../includes/classes/core.php');
            $data[data] = '';
            $req = mysql_query("SELECT * FROM `transporter_company` where actived = 1");
            $id=2;
            while( $res = mysql_fetch_assoc($req)){
                if($res['id'] == $id){
                    $data[data] .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
                } else {
                    $data[data] .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
                }
            }           
        break;    
    case 'get_list':
        require_once ('../../includes/classes/core.php');
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];

	    $rResult = mysql_query("SELECT	IFNULL(CONCAT('A-', a_letter_id), CONCAT('D-',d_letter_id)) AS id,
                                		IFNULL(CONCAT('ა - ', a_letter_id), CONCAT('დ - ',d_letter_id)) AS later,
                                		persons.`name` AS driver,
                                		object.`name` AS  car
                                FROM `warehouse`
                                LEFT JOIN a_letter 					ON a_letter.id	=	a_letter_id AND a_letter.actived=2
                                LEFT JOIN d_letter 					ON d_letter.id	=	d_letter_id AND d_letter.actived=2
                                JOIN	persons						ON persons.id		= (IFNULL(d_letter.receiver1, a_letter.receiver1))
                                LEFT JOIN  object					ON object.id 		= (IFNULL(d_letter.car_id, a_letter.car_id))
	        UNION 
	        SELECT	IFNULL(CONCAT('A-', a_letter_id), CONCAT('D-',d_letter_id)) AS id,
                                		IFNULL(CONCAT('ა - ', a_letter_id), CONCAT('დ - ',d_letter_id)) AS later,
                                		persons.`name` AS driver,
                                		object.`name` AS  car
                                FROM `warehouse`
                                LEFT JOIN a_letter 					ON a_letter.id	=	a_letter_id AND a_letter.actived=2
                                LEFT JOIN d_letter 					ON d_letter.id	=	d_letter_id AND d_letter.actived=2
                                JOIN	persons						ON persons.id		= (IFNULL(d_letter.receiver, a_letter.receiver))
                                LEFT JOIN  object					ON object.id 		= (IFNULL(d_letter.car_id, a_letter.car_id))
	        
	        ");

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{

				$data['aaData'][] = $aRow;
			}

        break;
        case 'get_list1':
            require_once ('../../includes/classes/core.php');
            $result = mysql_query("SELECT        persons.`name`, persons.`name`,
                                    			 CONCAT('<div class=',
                                    							IF(COUNT(DISTINCT a_letter.id)=0,'red',
                                    									IF(COUNT(DISTINCT a_letter.id)<6,'yellow','green'))
                                    							,'>',COUNT(DISTINCT a_letter.id),'</div>'),
                                    			 CONCAT('<div class=',
                                    						IF(COUNT(DISTINCT d_letter.id)=0,'red',
                                    									IF(COUNT(DISTINCT d_letter.id)<6,'yellow','green'))
                                    							,'>',COUNT(DISTINCT d_letter.id),'</div>')
                                    FROM persons
                                    LEFT JOIN a_letter ON	 a_letter.receiver1 = persons.id AND a_letter.actived=2
                                    LEFT JOIN d_letter ON	 d_letter.receiver1 = persons.id AND d_letter.actived=2
                                    WHERE persons.position=9
                                    GROUP BY persons.`name`");

            while ( $aRow = mysql_fetch_array( $result ) )
            {

                $data['aaData'][] = $aRow;
            }

            break;
     case 'check':
         $data['d'] = 'd';
     break;
     case 'clear_db':
         require_once ('../../includes/classes/core.php');
         $latter=strtolower($_REQUEST[table]);
         // mysql_query
         mysql_query("DELETE FROM `".$latter."_letter_details` WHERE (`".$latter."_letter_id`='$_REQUEST[letter]')");
         mysql_query("DELETE FROM `warehouse_detail` WHERE (`warehouse_id`='$_REQUEST[w_id]')");

         $data['d'] = 'd';


            break;

    default:
       $error = 'Action is Null';
}

if ($include!='') {
   require_once $include;

}else {

    $data['error'] = $error;
    echo json_encode($data);
}

return 0;
